﻿@using PMS.Data.Entities.SystemModules
@model ModuleButtonGroup
@{
    int ModuleId = (int)ViewBag.ModuleId;
    bool DisableUnauthorizedButtons = (bool)ViewBag.ButtonDisabled;
    var moduleAssignedBtns = ViewBag.ModuleAssignedBtns as IList<ModuleButton> ?? null;
    var moduleAssignedBtnIdsWithAccess = ViewBag.ModuleAssignedBtnIdsWithAccess as IList<(int btnId, bool canAccess)> ?? null;
    bool isBtnAuthed = false;
    string templetIdName = Model switch
    {
        ModuleButtonGroup.HeadButtonsGroup => "pmsHeadbarTpl",
        ModuleButtonGroup.RecordButtonsGroup => "pmsRecordBarTpl",
        _ => "pmsBarTpl"
    };
}
@if (moduleAssignedBtns?.Any() ?? false)
{
    <script type="text/html" id="@templetIdName">
        @foreach (var button in moduleAssignedBtns)
        {
            if (!button.AuthorizeRequired) isBtnAuthed = true;
            else if (moduleAssignedBtnIdsWithAccess == null) isBtnAuthed = false;
            else
            {

                isBtnAuthed = moduleAssignedBtnIdsWithAccess.FirstOrDefault(p => p.btnId == button.Id).canAccess;
            }
            <button type="button"
                    class="@((!isBtnAuthed && DisableUnauthorizedButtons) ? "layui-btn-disabled" : "") @Html.Raw(button.HtmlButtonCSSClass)"
                    lay-event="@button.EventName"
                    data-moduleid="@ModuleId"
                    data-buttonid="@button.Id">
                @if (button.DisplayMode == ModuleButtonDisplayMode.IconOnly && !string.IsNullOrEmpty(button.HtmlIconCSSClass))
                {
                    <i class="@button.HtmlIconCSSClass"></i>
                }
                else if (button.DisplayMode == ModuleButtonDisplayMode.DisplayNameAndIconBoth && !string.IsNullOrEmpty(button.HtmlIconCSSClass))
                {
                    <i class="@button.HtmlIconCSSClass"></i>@button.DisplayName
                }
                else
                {
                    @button.DisplayName
                }
            </button>
        }
    </script>
}
